package com.qqtech.qquan.user.controller;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.user.enums.UserConst;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.model.UserShield;
import com.qqtech.qquan.user.model.UserShieldQuery;
import com.qqtech.qquan.user.model.UserShieldVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserShieldService;
import com.qqtech.qquan.user.service.UserSysDictService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/user/shield")
public class UserShieldController extends BaseWebController<UserShield, UserShieldQuery> {
	@Resource
	private UserShieldService userShieldService;
	
	@Resource
	private UserSysDictService userSysDictService;
	
	@Resource
	private UserBaseService userBaseService;
    
	@Override
	protected BaseService<UserShield> getBaseService() {
		return userShieldService;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(@RequestBody UserShieldVo userShieldVo) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		userShieldVo.setCreatorId(curUserId);
		UserShieldVo userShieldVo2 = userShieldService.getOne(userShieldVo, new PageOrder());
		if(null!=userShieldVo2){
			ret.addErr("已经屏蔽了，无需重复屏蔽。");
			return ret;
		}
		userShieldService.insert(userShieldVo);
		return ret;
	}
	
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(@RequestBody UserShieldVo userShieldVo) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		userShieldVo.setCreatorId(curUserId);
		UserShieldVo userShieldVo2 = userShieldService.getOne(userShieldVo, new PageOrder());
		if (null != userShieldVo2) {
			userShieldService.delete(userShieldVo);
		}
		return ret;
	}
	
	/**
	 * 分页
	 * 
	 * @param userMoneyLogQuery
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/managePage")
	@ResponseBody
	public Result managePage(@RequestBody UserShieldQuery userShieldQuery) {
		Result ret = new Result();
		Integer curUserId = super.getUserId();
		userShieldQuery.setCreatorId(curUserId);
		Pageable pageKit = new PageKit(userShieldQuery.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<UserShieldVo> page = userShieldService.queryPageList(userShieldQuery, pageKit);
		String typeDesc=null;
		UserBaseVo userBaseVo=null;
		for (UserShieldVo userShieldVo : page) {
			userBaseVo=userBaseService.getById(userShieldVo.getShieldUserId());
			userShieldVo.setShieldUserNick(userBaseVo.getNick());
			userShieldVo.setShieldUserLogo(userBaseVo.getFullLogo());
			typeDesc=userSysDictService.dictValue(UserConst.DICTYPE_USER_SHIELD_TYPE, String.valueOf(userShieldVo.getType()));
			userShieldVo.setTypeDesc(typeDesc);
		}
		ret.put("page", page);
		return ret;
	}
}
